package nio.zerocopy;

import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author: Dgg
 * @Date: 2021/12/6 21:40
 */
public class NewIoClient {
	public static void main(String[] args) throws Exception {
		SocketChannel open = SocketChannel.open();
		open.connect(new InetSocketAddress("127.0.0.1", 7001));
		String fileName = "C:\\Users\\Doushaofei\\Desktop\\re-wx\\reli.zip";

		FileInputStream fileInputStream = new FileInputStream(fileName);

		FileChannel channel = fileInputStream.getChannel();

		//准备发送
		long startTime = System.currentTimeMillis();
		//在linux下一个transferTo 就可以完成传输
		//在windos 下 一次调用 transferTo 只能发送8m，就需要分段传输,而且要注意传输时的位置
		//transferTo 底层使用 零拷贝
		long l = channel.transferTo(0, channel.size(), open);

		System.out.println("发送总的字节数:" + l);
		System.out.println("耗时:" + (System.currentTimeMillis() - startTime));
	}
}
